package com.alpha.runes.util;

import com.alpha.runes.Common;
import com.alpha.runes.core.RunesManager;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;

public class OKGoUtil {

    private static OKGoUtil okGoUtil;

    private OKGoUtil() {
    }

    public static OKGoUtil getInstance() {
        if (okGoUtil == null) {
            synchronized (RunesManager.class) {
                okGoUtil = new OKGoUtil();
            }
        }
        return okGoUtil;
    }

    /**
     * 网络请求
     *
     * @param stringCallback
     */
    public void postRequest(String appKey, String channelCode, StringCallback stringCallback) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put(Common.ALPHA_SPEED_APPKEY, appKey);
            jsonObject.put(Common.ALPHA_SPEED_CHANNELCODE, channelCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        MediaType JSON = MediaType.parse("application/json; charset=utf-8");
        RequestBody body = RequestBody.create(JSON, jsonObject.toString());

        OkGo.<String>post(Common.URL)//
                .upRequestBody(body)
                .execute(stringCallback);
    }
}
